package com.zhuizikejithreeig.cn.threeigkeji.loginfo;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.zhuizikejithreeig.cn.content.ContectUtils;
import com.zhuizikejithreeig.cn.threeigkeji.BasicActivity;
import com.zhuizikejithreeig.cn.threeigkeji.CompontApplayCation;
import com.zhuizikejithreeig.cn.threeigkeji.MainActivity;
import com.zhuizikejithreeig.cn.threeigkeji.R;
import com.zhuizikejithreeig.cn.utils.AppManager;
import com.zhuizikejithreeig.cn.utils.ToastMake;
import com.zhuizikejithreeig.cn.utils.Utils;

import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;

public class BundingSetActivity extends BasicActivity implements View.OnClickListener {

    private static final int READSDK = 107;
    private Button mButton;
    private String mPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView_V(R.layout.activity_bunding_set);
        showBody();
        mButton = (Button) findViewById(R.id.name8);
        mButton.setOnClickListener(this);
        mPhone = Utils.getString(ContectUtils.PHONESAVE);

    }

    @Override
    public void onClick(View v) {
        if (ContextCompat.checkSelfPermission(BundingSetActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ToastMake.showToast(getResources().getString(R.string.string69));
            hasReadPermission();

        } else {
            if (TextUtils.isEmpty(mPhone) && !Utils.isPhoneNO(mPhone)) {
                skipOtherActivity(LoginActivity.class, true);
                CompontApplayCation.loginNeedFinsh = false;
                return;
            }
            skipOtherActivity(SaoMaActivity.class, false);
        }
    }

    private void hasReadPermission() {
        PermissionGen.with(BundingSetActivity.this)
                .addRequestCode(READSDK)
                .permissions(
                        Manifest.permission.CAMERA
                )
                .request();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @PermissionSuccess(requestCode = READSDK)
    public void requestPhotoSuccess() {
        //成功之后的处理
        //.......
        skipOtherActivity(SaoMaActivity.class, false);
    }

    @PermissionFail(requestCode = READSDK)
    public void requestPhotoFail() {
        //失败之后的处理，我一般是跳到设置界面
        boolean showRequestPermission = ActivityCompat.shouldShowRequestPermissionRationale(BundingSetActivity.this, Manifest.permission.CAMERA);
        if (showRequestPermission) {
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(Uri.parse("package:" + getPackageName()));
            startActivity(intent);
        }
    }

    @Override
    public void onBackPressed() {
//        Intent intent = new Intent(BundingSetActivity.this, MainActivity.class);
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//        startActivity(intent);
        Activity activity = AppManager.getInstance().getActivity(LoginActivity.class);
        Activity mBactivity = AppManager.getInstance().getActivity(BasicInfoActivity.class);
        if (activity != null) {
            activity.finish();
        }
        if (mBactivity != null) {
            mBactivity.finish();
        }
        skipOtherActivity(MainActivity.class, true);
        finish();
    }
}
